package com.e_manager.soft.emanager.controller;

import com.alibaba.fastjson.JSON;
import com.e_manager.soft.emanager.annotation.BaseController;
import com.e_manager.soft.emanager.entity.Equipment;
import com.e_manager.soft.emanager.entity.GHIJson;
import com.e_manager.soft.emanager.entity.GHIJsonArr;
import com.e_manager.soft.emanager.entity.User;
import com.e_manager.soft.emanager.resultMsg.ResultConstant;
import com.e_manager.soft.emanager.resultMsg.ResultInfo;
import com.e_manager.soft.emanager.service.GHIJsonService;
import com.e_manager.soft.emanager.service.MstMessageService;
import com.e_manager.soft.emanager.util.DataTransformationUtil;
import com.e_manager.soft.emanager.util.DateUtil;
import com.e_manager.soft.emanager.util.XingeUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : lip[lip4051@163.com]
 * @create : 2018/3/20 0020 下午 1:25
 */
@BaseController
public class GHIJsonController {
    @Value("${ANDROID_SUPPLY_ACCESS_ID}")
    private Long accessId;

    @Value("${ANDROID_SUPPLY_SECRET_KEY}")
    private String secretKey;
    @Value("${ANDROID_DEVICE}")
    private String android;
    @Value("${IOS_DEVICE}")
    private String ios;
    @Autowired
    private GHIJsonService ghiJsonService;
    @Autowired
    private MstMessageService mstMessageService;

    @ApiOperation(value = "异构网络转发json格式", notes = "保存json数据")
    @RequestMapping(value = "/insertGHIJson", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    @SuppressWarnings("unchecked")
    public ResultInfo<GHIJson> insertGHIJsion(@RequestBody GHIJson Gjson) {
        //初始化对象
        ResultInfo<GHIJson> info = new ResultInfo<>();
        Map map = new HashMap();
        try {
            //判断是否正常数据
            List<GHIJsonArr> GHIlist = Gjson.getNodeSensors();
            GHIJsonArr ghiJsonArr = new GHIJsonArr();
            String voltage = "";
            String date = "";
            ghiJsonArr.setNodeOID(Gjson.getNodeOID());
            for (GHIJsonArr ghiJson : GHIlist) {
                switch (ghiJson.getSensorType()) {
                    case "0000":
                        break;
                    case "0001":
                        ghiJsonArr.setTemperature(ghiJson.getSdValue());
                        date = ghiJson.getUpdateTime();
                        ghiJsonArr.setOcValue(ghiJson.getOcValue());
                        break;
                    case "0002":
                        ghiJsonArr.setHumidity(ghiJson.getSdValue());
                        date = ghiJson.getUpdateTime();
                        ghiJsonArr.setOcValue(ghiJson.getOcValue());
                        break;
                    case "0047":
                        voltage = ghiJson.getOcValue();
                        ghiJsonArr.setVoltage(ghiJson.getSdValue());
                        ghiJsonArr.setOcValue(voltage);
                        date = ghiJson.getUpdateTime();
                        break;
                }
            }
            if (!"".equals(voltage)) {
                if (voltage.substring(0, 2).equals("00")) {
                    //设置报文类型
                    ghiJsonArr.setMessageType("0");
                    //设置报警类型
                    ghiJsonArr.setWarnType("");
                    //取设备时间
                    ghiJsonArr.setUpdateTime(DateUtil.dateToString(DateUtil.fomatDate(date)));
                } else {
                    //取报警数据
                    Map resultMap = DataTransformationUtil.dataStrToMap(voltage);
                    //设置报警类型
                    ghiJsonArr.setWarnType(String.valueOf(resultMap.get("warnType")));
                    //取设备时间
                    ghiJsonArr.setUpdateTime(DateUtil.dateToString(DateUtil.fomatDate(date)));
                    //设置报文类型
                    ghiJsonArr.setMessageType("1");
                }
            } else {
                //设置报文类型
                ghiJsonArr.setMessageType("0");
                //设置报警类型
                ghiJsonArr.setWarnType("");
                //取设备时间
                ghiJsonArr.setUpdateTime(DateUtil.dateToString(DateUtil.fomatDate(date)));
            }
            map.put("voltage", ghiJsonArr.getVoltage());
            map.put("temperature", ghiJsonArr.getTemperature());
            map.put("humidity", ghiJsonArr.getHumidity());
            map.put("warnType", ghiJsonArr.getWarnType());
            map.put("messageType", ghiJsonArr.getMessageType());
            map.put("messageDate", ghiJsonArr.getUpdateTime());
            new Thread(new Runnable() {
                public void run() {
                    //插入数据库
                    ghiJsonService.insertGHI(ghiJsonArr);
                    info.setMsg(ResultConstant.SUCCESS.getMessage());
                    info.setCode(ResultConstant.SUCCESS.getCode());
                }
            }).start();
            new Thread(new Runnable() {
                public void run() {
                    //初始化对象
                    Equipment equipment = new Equipment();
                    //取设备编码
                    equipment.setEquipmentCode(Gjson.getNodeOID());
                    //根据设备取信鸽token
                    List<User> xinToken = mstMessageService.queryXinToken(equipment);
                    if (xinToken.size() > 0) {
                        //循环推送
                        for (int i = 0; i < xinToken.size(); i++) {
                            if (xinToken.get(i).getXinToken() != null && !"".equals(xinToken.get(i).getXinToken())) {
                                map.put("equipmentName", xinToken.get(i).getEquipmentName());
                                map.put("equipmentCode", Gjson.getNodeOID());
                                if (map.get("messageType").equals("0")) {
                                    String devStr = "android";
                                    if (android.equals(xinToken.get(i).getXinDev())) {
                                        devStr = android;
                                    } else {
                                        devStr = ios;
                                    }
                                    XingeUtils.pushTokenAndroid(accessId, secretKey, "Data", JSON.toJSONString(map), String.valueOf(xinToken.get(i).getXinToken()), 0, devStr);
                                } else {
                                    String devWarning = "android";
                                    if (android.equals(xinToken.get(i).getXinDev())) {
                                        devWarning = android;
                                    } else {
                                        devWarning = ios;
                                    }
                                    XingeUtils.pushTokenAndroid(accessId, secretKey, "E温馨提示：屋内环境出现异常请您及时查看！", JSON.toJSONString(map), String.valueOf(xinToken.get(i).getXinToken()), 1, devWarning);
                                    XingeUtils.pushTokenAndroid(accessId, secretKey, "Data", JSON.toJSONString(map), String.valueOf(xinToken.get(i).getXinToken()), 0, devWarning);
                                }
                            }
                        }
                    }

                }
            }).start();
        } catch (Exception e) {
            info.setMsg(ResultConstant.INSERT_FAIL.getMessage());
            info.setCode(ResultConstant.INSERT_FAIL.getCode());
            e.printStackTrace();
        }
        return info;
    }
}
